﻿(function ($) {

    //$('#pager').paging(20,2,7);//总页数,当前页,显示页码数
    //$('#pager').paging(100,12,2,7);//总条数,页大小,当前页,显示页码数

    $.fn.paging = function () {
        var $this = $(this);

        var page = arguments[arguments.length - 2] - 1, show = arguments[arguments.length - 1], total = arguments[0];

        if (arguments.length > 3) total = Math.ceil(arguments[0] / arguments[1]);

        if (total <= 0) {
            $this.empty();
        } else {
            var sb = [];

            var baseNum = parseInt(page / show) * show;

            sb.push('<a href="javascript:void(0)" class="prev ' + (page == 0 ? "prev_no" : '') + '">上一页</a>');

            for (var i = baseNum; i <= Math.min(baseNum + show, total) - 1; i++) {
                if (i != page) {
                    sb.push('<a index="' + i + '" href="javascript:void(0)" class="item">' + (i + 1) + '</a>');
                } else {
                    sb.push('<span class="current">' + (i + 1) + '</span>');
                }
            }

            sb.push('<a href="javascript:void(0)" class="next ' + (page == total - 1 ? "prev_no" : '') + '">下一页</a>');
            sb.push('<span class="total"> 共' + total + ' 页</span>');
            sb.push('<span class="go-text">第 <input class="text page" type="text"> 页</span>');
            sb.push('<a class="btn-go" href="javascript:void(0)">确定</a>');
            //sb.push('<span class="go-text">To<input type="text" class="text" /></span><a href="#" class="btn-go">Skip</a>');
            //<span class="go-text">To<input type="text" class="text" /></span><a href="#" class="btn-go">Skip</a>

            $this.empty().append(sb.join(''));

            $this.find('a.item').click(function () {
                page = parseInt($(this).attr('index'));
                $.publish('pageChanged', { total: total, page: page + 1 });
            });

            $this.find('a.next').click(function () {
                var nextIndex = page + 1;
                if (nextIndex < total) {
                    page = nextIndex;
                    $.publish('pageChanged', { total: total, page: page + 1 });
                }
            });

            $this.find('a.prev').click(function () {
                var prevIndex = page - 1;
                if (prevIndex >= 0) {
                    page = prevIndex;
                    $.publish('pageChanged', { total: total, page: page + 1 });
                }
            });

            $this.find('a.btn-go').click(function () {
                var gotoPage = parseInt($this.find('.page').val()) - 1;
                if (gotoPage >= 0 && gotoPage < total && gotoPage != page) {
                    page = gotoPage;
                    $.publish('pageChanged', { total: total, page: page + 1 });
                }
            });

            $this.find('.page').keydown(function (e) {
                var curKey = e.which;
                if (curKey == 13) {
                    var gotoPage = parseInt($this.find('.page').val()) - 1;
                    if (gotoPage >= 0 && gotoPage < total && gotoPage != page) {
                        page = gotoPage;
                        $.publish('pageChanged', { total: total, page: page + 1 });
                    }

                }
            });
        }

        $this.page = page;

        $this.total = total;

        return $this;
    }
})(jQuery);